package com.heima.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.common.util.JsonUtils;
import com.heima.model.search.dtos.UserSearchDto;
import com.heima.search.entity.ApAssociateWords;
import com.heima.search.mapper.ApAssociateWordsMapper;
import com.heima.search.model.Trie;
import com.heima.search.service.IApAssociateWordsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 联想词表 服务实现类
 * </p>
 *
 * @author heima
 * @since 2021-03-27
 */
@Service
public class ApAssociateWordsServiceImpl extends ServiceImpl<ApAssociateWordsMapper, ApAssociateWords> implements IApAssociateWordsService {

    @Override
    public List<ApAssociateWords> findAssociateList(UserSearchDto dto) {
        //创建查询条件
        QueryWrapper<ApAssociateWords> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(ApAssociateWords::getAssociateWords,dto.getSearchWords());
        //设置分页条件
        Page<ApAssociateWords> page = new Page<>(dto.getPageNum(),dto.getPageSize());
        IPage<ApAssociateWords> iPage = page(page, queryWrapper);
        if(CollectionUtils.isEmpty(iPage.getRecords())){
            throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        return iPage.getRecords();
    }

    @Autowired
    private StringRedisTemplate redisTemplate;

    String PRE_FIX = "ld:search:associate:";

    @Override
    public List<ApAssociateWords> findAssociateListV2(UserSearchDto dto) {
        //尝试从redis中获取联想词列表
        String key=PRE_FIX+"list";
        String associateListStr = redisTemplate.opsForValue().get(key);
        List<ApAssociateWords> list = null;
        if(StringUtils.isBlank(associateListStr)){
            //redis没有联想词列表，查询数据库，再将数据库查询结果缓存到redis
            list = list();
            redisTemplate.opsForValue().set(key, JsonUtils.toString(list));
        }else{
            //解析redis的联想词列表
            list =JSON.parseArray(associateListStr,ApAssociateWords.class);
        }

        //构建联想词 trie数据结构
        Trie trie = new Trie();
        for (ApAssociateWords apAssociateWords : list) {
            trie.insert(apAssociateWords.getAssociateWords());
        }
        //查询搜索关键字相关的联想词列表
        List<String> list1 = trie.startWith(dto.getSearchWords());

        List<ApAssociateWords> showList = new ArrayList<>();
        for (String s : list1) {
            ApAssociateWords apAssociateWords = new ApAssociateWords();
            apAssociateWords.setAssociateWords(s);
            showList.add(apAssociateWords);
        }
        return showList;
    }
}
